package iris.managers
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.core.IFlexDisplayObject;
	import mx.core.IFlexModuleFactory;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.effects.Blur;
	import mx.effects.Effect;
	import mx.effects.EffectManager;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	public class IrisPopUpManager
	{
		public static const EFFECT_BLUR:String = "effectBlur";

		public static const LOCATE_MOUSE:String = "locateMouse";
		public static const LOCATE_PARENT_CENTER:String = "locateParentCenter";

		private static var blurCreate:Effect = getBlurCreate();
		private static var blurRemove:Effect = getBlurRemove();

		private static function getBlurCreate():Effect
		{
			var blur:Blur = new Blur();
			blur.blurYTo = 0;
			blur.blurXTo = 0;
			blur.blurXFrom = 18;
			blur.blurYFrom = 18;
			blur.duration = 350;
			return blur;
		}

		private static function getBlurRemove():Effect
		{
			var blur:Blur = new Blur();
			blur.blurYTo = 12;
			blur.blurXTo = 12;
			blur.duration = 200;
			return blur;
		}

		public static function createPopUp(parent:DisplayObject, className:Class, modal:Boolean = false, childList:String = null,
			locate:String = null, effect:String = null):IFlexDisplayObject
		{
			var popUp:IFlexDisplayObject = PopUpManager.createPopUp(parent, className, modal, childList);

			if(popUp is UIComponent)
			{
				var ui:UIComponent = popUp as UIComponent;
				ui.addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void
					{
						if(locate == LOCATE_MOUSE)
						{
							popUp.x = (Application.application as DisplayObject).mouseX;
							popUp.y = (Application.application as DisplayObject).mouseY;
						}
						else if(locate == LOCATE_PARENT_CENTER)
						{
							PopUpManager.centerPopUp(popUp);
						}

						if(effect == EFFECT_BLUR)
							blurCreate.play([event.target]);
					}, false, 0, false);
			}
			return popUp;
		}

		public static function removePopUp(popUp:IFlexDisplayObject, effect:String = null):void
		{
			if(effect != null)
			{
				EffectManager.endEffectsForTarget(popUp as IUIComponent);
				if(effect == EFFECT_BLUR)
				{
					blurRemove.addEventListener(EffectEvent.EFFECT_END, function():void
						{
							doRemovePopUp(popUp)
						}, false, 0, false);
					popUp.addEventListener(MouseEvent.CLICK, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.DOUBLE_CLICK, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_DOWN, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_MOVE, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_OUT, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_OVER, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_UP, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(MouseEvent.MOUSE_WHEEL, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(KeyboardEvent.KEY_DOWN, popUp_interactiveHandler, true, int.MAX_VALUE);
					popUp.addEventListener(KeyboardEvent.KEY_UP, popUp_interactiveHandler, true, int.MAX_VALUE);
					blurRemove.play([popUp]);
				}
			}
			else
			{
				doRemovePopUp(popUp);
			}
		}

		private static function popUp_interactiveHandler(event:Event):void
		{
			event.stopImmediatePropagation();
		}

		private static function doRemovePopUp(popUp:IFlexDisplayObject):void
		{
			PopUpManager.removePopUp(popUp);
		}
	}
}